from common import get_logger
from douyin_utils import crawl_douyin_videos, extract_audio
from cos_utils import upload_to_cos
from asr_utils import audio_to_text
from xhs_utils import douyin_to_xhs_text
from image_utils import generate_images_by_text
from xhs_publish import post_to_xhs

logger = get_logger(__name__)


def main():
    logger.info("流程开始")
    try:
        # 1.爬取热门视频
        video_files = crawl_douyin_videos("脆桃")
        for video_file in video_files:
            logger.info(f"开始处理视频文件:{video_file}")
            # 2.把视频转成音频
            mp3_file = extract_audio(video_file)
            logger.info(f"音频地址:{mp3_file}")
            # 3.上传音频到腾讯云COS
            cos_url = upload_to_cos(mp3_file)
            logger.info(f"上传音频到腾讯COS成功:{cos_url}")
            # 4.把音频转成文字
            douyin_text = audio_to_text(cos_url)
            logger.info(f"音频转文字成功:{douyin_text}")
            # 5.把抖音的文案改为小红书的爆款文案
            xhs_title, xhs_intro, xhs_tags = douyin_to_xhs_text(douyin_text)
            logger.info(
                f"转小红书文案:xhs_title:{xhs_title} xhs_intro:{xhs_intro} xhs_tags:{xhs_tags}"
            )
            # 6.根据文案生成图片
            img_paths = generate_images_by_text(xhs_title, xhs_intro)
            logger.info(f"图片生成成功:{img_paths}")
            # 7.发到内容到小红书里
            post_to_xhs(
                title=xhs_title, intro=xhs_intro, tags=xhs_tags, img_paths=img_paths
            )
            # 8.发布内容到知乎尽量再找一个平台 比如头条号 百家号

    except Exception as e:
        logger.error(f"主流程执行出错:{e}")


if __name__ == "__main__":
    main()
